package com.freightoa.foundation.presentation.activity

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.core.view.WindowCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.freightoa.foundation.presentation.theme.SGLTheme
import com.freightoa.foundation.viewmodel.BaseState
import com.freightoa.foundation.viewmodel.BaseViewModel
import com.freightoa.foundation.viewmodel.UiIntent


// 定义 BaseActivity
//@AndroidEntryPoint
abstract class BaseComposeActivity<State : BaseState, Intent : UiIntent, VM : BaseViewModel<State, Intent>> :
    ComponentActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            val viewModel: VM = ViewModelProvider(this)[getViewModelClass()]
            val state by viewModel.uiState.collectAsStateWithLifecycle(
                initialValue = createInitialState(),
                lifecycle = lifecycle,
                minActiveState = Lifecycle.State.STARTED
            )
            SGLTheme {
                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                    Content(viewModel,state, viewModel::handleIntent, Modifier.padding(innerPadding))
                }
            }

        }
    }

    // 抽象方法，用于获取 ViewModel 的 Class 对象
    abstract fun getViewModelClass(): Class<VM>

    // 抽象方法，用于创建初始状态，具体实现由子类完成
    abstract fun createInitialState(): State

    @Composable
    abstract fun Content(
        viewModel: VM,
        state: State,
        sendIntent: (Intent) -> Unit,
        modifier: Modifier
    )
}